from utils.excel_util import ExcelUtil
from model.sysJobLogModel import SysJobLog
from schema.sysJobLogSchema import SysJobLogCreate
from typing import List, Any, Dict, ClassVar
from io import BytesIO
from datetime import datetime


class SysJobLogExcel:
    # Define export columns
    COLUMNS: ClassVar[List[Dict[str, Any]]] = [

        {"field": "job_name", "title": "任务名称", "required": True},

        {"field": "job_group", "title": "任务组名", "required": True},

        {"field": "invoke_target", "title": "调用目标字符串", "required": True},

        {"field": "job_message", "title": "日志信息", "required": False},

        {"field": "exec_status", "title": "执行状态（0正常 1失败）", "required": False},

        {"field": "status", "title": "状态（0正常 2删除）", "required": False},

        {"field": "exception_info", "title": "异常信息", "required": False},

        {"field": "create_by", "title": "创建者", "required": False},

        {"field": "create_time", "title": "创建时间", "required": False},

        {"field": "update_by", "title": "更新者", "required": False},

        {"field": "update_time", "title": "更新时间", "required": False},

        {"field": "remark", "title": "备注信息", "required": False},

    ]

    @classmethod
    def get_export_columns(cls) -> List[Dict[str, Any]]:
        return [

            {"field": "job_name", "title": "任务名称"},

            {"field": "job_group", "title": "任务组名"},

            {"field": "invoke_target", "title": "调用目标字符串"},

            {"field": "job_message", "title": "日志信息"},

            {"field": "exec_status", "title": "执行状态（0正常 1失败）"},

            {"field": "status", "title": "状态（0正常 2删除）"},

            {"field": "exception_info", "title": "异常信息"},

            {"field": "create_by", "title": "创建者"},

            {"field": "create_time", "title": "创建时间"},

            {"field": "update_by", "title": "更新者"},

            {"field": "update_time", "title": "更新时间"},

            {"field": "remark", "title": "备注信息"},

        ]

    @classmethod
    def get_import_template(cls) -> BytesIO:
        """Get import template"""
        example_data = [{

            "job_name": "example",

            "job_group": "example",

            "invoke_target": "example",

            "job_message": "example",

            "exec_status": "A",

            "status": "A",

            "exception_info": "example",

            "create_by": "example",

            "create_time": datetime.now(),

            "update_by": "example",

            "update_time": datetime.now(),

            "remark": "example",

        }]
        return ExcelUtil.create_import_template(
            columns=cls.COLUMNS,
            example_data=example_data,
            sheet_name="定时任务调度日志表导入模板"
        )

    @classmethod
    async def export_data(cls, data: List[Any]) -> BytesIO:
        """Export data"""
        return await ExcelUtil.export_data(
            data=data,
            columns=cls.get_export_columns(),
            sheet_name="定时任务调度日志表数据"
        )

    @classmethod
    def parse_import_data(cls, file: BytesIO) -> List[Dict[str, Any]]:
        """Parse import data"""
        return ExcelUtil.parse_import_data(
            file=file,
            columns=cls.COLUMNS,
            schema_cls=SysJobLogCreate
        )